package org.misty.blog.controller;

import org.misty.blog.exception.NotFoundException;
import org.misty.blog.po.Tag;
import org.misty.blog.service.TagService;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;
import java.util.Optional;

@Controller
@RequestMapping("/admin")
public class TagController {
    private final TagService tagService;

    public TagController(TagService tagService) {
        this.tagService = tagService;
    }

    @GetMapping("/tags")
    public String tags(
            @PageableDefault(size = 10, sort = {"id"}, direction = Sort.Direction.DESC) Pageable pageable,
            Model model
    ) {
        model.addAttribute("page", tagService.listTags(pageable));
        return "admin/tags";
    }

    @GetMapping("/tags/input")
    public String input(Model model) {
        model.addAttribute("tag", new Tag());
        return "admin/tags-input";
    }

    @GetMapping("/tags/{id}/input")
    public String edit(@PathVariable Long id, Model model) {
        Optional<Tag> old = tagService.getTag(id);
        Tag tag = old.orElseThrow(() -> new NotFoundException("<标签>找不到当前对象: " + id));
        model.addAttribute("tag", tag);
        return "admin/tags-input";
    }

    @PostMapping("/tags")
    public String post(@Valid Tag tag, BindingResult result, RedirectAttributes attributes) {
        Optional<Tag> old = tagService.getTagByName(tag.getName());
        old.ifPresent(tag1 -> result.rejectValue("name", "nameError", "标签名称已存在: " + tag.getName()));
        if (result.hasErrors()) {
            return "admin/tags-input";
        }
        Tag ret = tagService.saveTag(tag);
        if (ret == null) {
            attributes.addFlashAttribute("message", "新增失败");
        } else {
            attributes.addFlashAttribute("message", "新增成功");
        }
        return "redirect:/admin/tags";
    }

    @PostMapping("/tags/{id}")
    public String editPost(@PathVariable Long id, @Valid Tag tag, BindingResult result, RedirectAttributes attributes) {
        Optional<Tag> old = tagService.getTagByName(tag.getName());
        old.ifPresent(tag1 -> result.rejectValue("name", "nameError", "标签名称已存在: " + tag.getName()));
        if (result.hasErrors()) {
            return "admin/tags-input";
        }
        Tag ret = tagService.updateTag(id, tag);
        if (ret == null) {
            attributes.addFlashAttribute("message", "修改失败");
        } else {
            attributes.addFlashAttribute("message", "修改成功");
        }
        return "redirect:/admin/tags";
    }

    @RequestMapping("/tags/{id}/delete")
    public String delete(@PathVariable Long id, RedirectAttributes attributes) {
        tagService.removeTag(id);
        attributes.addFlashAttribute("message", "删除成功");
        return "redirect:/admin/tags";
    }
}
